﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsItem>
#include <qgraphicsitem.h>
#include <qgraphicsscene.h>
#include <qnamespace.h>
#include <qobject.h>
#include <qpen.h>
#include <qpushbutton.h>
#include <qwindowdefs.h>

void MainWindow::iniGraphicsSystem()
{
    ui->View->setScene(scene);
    ui->View->setCursor(Qt::CrossCursor);
    ui->View->setMouseTracking(true);
    ui->View->setDragMode(QGraphicsView::RubberBandDrag);
    ui->View->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    ui->View->setViewportUpdateMode(QWGraphicsView::FullViewportUpdate);

    // 坐标十字
    QPen pen{QBrush{Qt::red}, 3, Qt::DotLine};
    QGraphicsLineItem *line = new QGraphicsLineItem{-50, 0, 50, 0};
    line->setZValue(100);
    line->setPen(pen);
    scene->addItem(line);
    line = new QGraphicsLineItem{0, -50, 0, 50};
    line->setZValue(100);
    line->setPen(pen);
    scene->addItem(line);

    // 中间的圆
    QAbstractGraphicsShapeItem *item = new QGraphicsEllipseItem(-100, -50, 200, 100);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    scene->addItem(item);

    // 左下的椭圆 1
    item = new QGraphicsEllipseItem(-50, -50, 100, 50);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    item->setPos(50, 50);
    scene->addItem(item);

    // 左下的椭圆 2
    item = new QGraphicsEllipseItem(0, 0, 100, 50);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    item->setPos(0, 0);
    scene->addItem(item);

    // 右侧的矩形
    item = new QGraphicsRectItem(100, -10, 140, 20);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    scene->addItem(item);

    // 左侧的矩形
    item = new QGraphicsRectItem(0, 0, 140, 20);
    item->setPos(-240, -10);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    scene->addItem(item);

    // 上方的一组矩形
    item = new QGraphicsRectItem(-50, -20, 100, 40);
    item->setPos(0, -100);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    scene->addItem(item);
    item = new QGraphicsRectItem(-10, -10, 40, 20, item);
    item->setPos(-30, 0);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);

    //下方的一组group
    auto *group = new QGraphicsItemGroup();
    item = new QGraphicsRectItem(-50, -20, 100, 40, group);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    item = new QGraphicsRectItem(-20, -10, 40, 20, group);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    item->setPos(-25, 0);
    item = new QGraphicsRectItem(-20, -10, 40, 20, group);
    item->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    item->setPos(25, 0);
    group->setPos(0, 100);
    group->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsFocusable);
    scene->addItem(group);

    scene->clearSelection();
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), scene(new QGraphicsScene(-100, -100, 200, 200)), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    labViewCord = new QLabel(QString{"View:"});
    labViewCord->setMinimumWidth(80);
    ui->statusBar->addWidget(labViewCord);

    labSceneCord = new QLabel(QString{"Scene:"});
    labSceneCord->setMinimumWidth(80);
    ui->statusBar->addWidget(labSceneCord);

    labItemCord = new QLabel(QString{"Item:"});
    labItemCord->setMinimumWidth(80);
    ui->statusBar->addWidget(labItemCord);

    labScenePosCord = new QLabel(QString{"scene pos:"});
    labScenePosCord->setMinimumWidth(100);
    ui->statusBar->addWidget(labScenePosCord);

    labPosCord = new QLabel(QString{"pos:"});
    labPosCord->setMinimumWidth(80);
    ui->statusBar->addWidget(labPosCord);

    QObject::connect(ui->View, SIGNAL(mouseMovePoint(QPoint)), this, SLOT(on_mouseMovePoint(QPoint)));
    QObject::connect(ui->View, SIGNAL(mouseClicked(QPoint)), this, SLOT(on_mouseClicked(QPoint)));
    QObject::connect(ui->pushButton_scene_left, &QPushButton::clicked, this, &MainWindow::on_sceneLeft);
    QObject::connect(ui->pushButton_scene_center, &QPushButton::clicked, this, &MainWindow::on_sceneCenter);
    QObject::connect(ui->pushButton_scene_right, &QPushButton::clicked, this, &MainWindow::on_sceneRight);
    QObject::connect(ui->pushButton_rotation, &QPushButton::clicked, this, &MainWindow::on_sceneRotation);

    iniGraphicsSystem();
    on_sceneCenter();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_sceneLeft()
{
    ui->View->setAlignment(Qt::AlignLeft | Qt::AlignTop);
}

void MainWindow::on_sceneCenter()
{
    ui->View->setAlignment(Qt::AlignCenter);
}

void MainWindow::on_sceneRight()
{
    ui->View->setAlignment(Qt::AlignRight | Qt::AlignBottom);
}

void MainWindow::on_sceneRotation()
{
    QList<QGraphicsItem *> items = scene->selectedItems();
    for (auto *item : items)
    {
        qreal rotation = item->rotation();
        item->setRotation(rotation + 90);
    }
}

void MainWindow::on_mouseMovePoint(QPoint point)
{
    labViewCord->setText(QString("View:%1 %2").arg(point.x()).arg(point.y()));
    QPointF pointScene = ui->View->mapToScene(point);
    labSceneCord->setText(QString("Scene:%1 %2").arg(pointScene.x()).arg(pointScene.y()));

    QGraphicsItem *item = nullptr;
    item = scene->itemAt(pointScene, ui->View->transform());
    if (item != nullptr)
    {
        QPointF pos = item->pos();
        QPointF scene_pos = item->scenePos();
        labPosCord->setText(QString("pos:%1 %2").arg(pos.x()).arg(pos.y()));
        labScenePosCord->setText(QString("scene pos:%1 %2").arg(scene_pos.x()).arg(scene_pos.y()));
    }
    else
    {
        labPosCord->setText(QString("pos"));
        labScenePosCord->setText(QString("scene pos"));
    }
}

void MainWindow::on_mouseClicked(QPoint point)
{
    QPointF pointScene = ui->View->mapToScene(point);
    QGraphicsItem *item = nullptr;
    item = scene->itemAt(pointScene, ui->View->transform());
    if (item != nullptr)
    {
        QPointF pointItem = item->mapFromScene(pointScene);
        labItemCord->setText(QString("Item:%1 %2").arg(pointItem.x()).arg(pointItem.y()));
    }
}
